package b.h.y.bj;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import b.h.y.bj.util.DensityUtil;
import b.h.y.bj.util.SharedPreferencesUtil;

/**
 * 新手引导页
 */
public class GuideActivity extends AppCompatActivity {

    private static final int[] mImgs = new int[]{
            R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3
    };

    private ViewPager vpGuide;
    private List<ImageView> mImagesViewList;
    private LinearLayout llPointGroup; //小圆点的布局

    private int originSize; //原点尺寸
    private View viewPointRed;
    private RelativeLayout.LayoutParams rParams; //红色小圆点的布局
    private Button btnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide);

        vpGuide = (ViewPager) findViewById(R.id.vp_guide);

        llPointGroup = (LinearLayout) findViewById(R.id.ll_apoint_group);

        viewPointRed = findViewById(R.id.view_red_point);

        btnStart = (Button) findViewById(R.id.btn_start);

        initView();

        vpGuide.setAdapter(new GuideAdapter());

        vpGuide.addOnPageChangeListener(new GuidePageListenter());
    }

    /**
     * 初始化界面
     */
    private void initView() {
        mImagesViewList = new ArrayList<ImageView>();

        //设置原点大小和间距
        originSize = DensityUtil.dip2px(this, 8);

        for (int i = 0, len = mImgs.length; i < len; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(mImgs[i]);
            mImagesViewList.add(imageView);

            //初始化引导页的小圆点
            View pointView = new View(this);
            pointView.setBackgroundResource(R.drawable.shape_point_gray); //这只默认的原点

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(originSize, originSize);
            params.setMargins(originSize, 0, originSize, 0);
            pointView.setLayoutParams(params); //设置引导页原点的大小
            llPointGroup.addView(pointView); //将小圆点添加给线性布局
        }
        rParams = new RelativeLayout.LayoutParams(originSize, originSize);
        rParams.leftMargin = originSize;
        viewPointRed.setLayoutParams(rParams);
    }

    /**
     * 点击开始体验跳转到主页面
     *
     * @param view
     */
    public void goApplication(View view) {
        Intent intent = new Intent(GuideActivity.this, MainActivity.class);
        startActivity(intent);

        SharedPreferencesUtil.setBoolean(GuideActivity.this, "is_show_guide", true);

        finish();
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImgs.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = mImagesViewList.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    /**
     * ViewPager滑动监听
     */
    class GuidePageListenter implements ViewPager.OnPageChangeListener {


        /**
         * 滑动的事件
         *
         * @param position             当前viewpager的位置
         * @param positionOffset       偏移的百分比
         * @param positionOffsetPixels 移动距离
         */
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int originDistance = originSize * 3; //原点距离 = 原点尺寸 + 左边距 + 右边距
            int offset = (int) (originDistance * positionOffset + position * originDistance + originSize); //计算后的偏移距离 = 偏移比例 + 根据不同位置的子view*原点距离 + 左边距
            rParams.leftMargin = offset;
            viewPointRed.setLayoutParams(rParams);
        }

        //选中的事件
        @Override
        public void onPageSelected(int position) {
            if (position == mImgs.length - 1) {
                btnStart.setVisibility(View.VISIBLE);
            } else {
                btnStart.setVisibility(View.INVISIBLE);
            }
        }

        //滑动状态的变化
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
